package com.chinasoft.cr.controller;

import com.chinasoft.cr.mapper.ReservationsMapper;
import com.chinasoft.cr.pojo.PageItem;
import com.chinasoft.cr.pojo.Reservations;
import com.chinasoft.cr.pojo.Result;
import com.chinasoft.cr.pojo.Venues;
import com.chinasoft.cr.pojo.vo.ReservationsParam;
import com.chinasoft.cr.service.ReservationsService;
import com.chinasoft.cr.service.VenuesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.tomcat.jni.Library;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预约管理前端控制器
 * </p>
 *
 * @author 你的名字
 * @since 2025-01-21
 */
@RestController
@CrossOrigin  // 解决跨域问题
@RequestMapping("/api/reservations")
@Tag(name = "预约管理", description = "用于管理预约信息")
public class ReservationsController {


    @Autowired
    private ReservationsService reservationsService;
    @Autowired
    private VenuesService venuesService;
    @Autowired
    private ReservationsMapper reservationsMapper;

    /**
     * 查询预约列表
     *
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 分页查询结果
     */
    @GetMapping(value = "/list")
    @Operation(summary = "分页获取所有预约信息", description = "分页查询所有预约的详细信息")
    @ApiResponse(responseCode = "200", description = "分页获取所有预约信息成功")
    public Result list(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String username,
            @RequestParam(required = false, defaultValue = "") String nickName,
            @RequestParam(required = false, defaultValue = "") String venueName,
            @RequestParam(required = false) String date,
            @RequestParam(required = false) String eventName,
            @RequestParam(required = false, defaultValue = "0") int userId,
            @RequestParam(required = false, defaultValue = "0") int venueId,
            @RequestParam(required = false, defaultValue = "0") int role,
            @RequestParam(required = false, defaultValue = "") String status
    ) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

//        Page<Reservations> aPage = reservationsService.page(new Page<>(pageNum, pageSize));

//        Page<Reservations> page = new Page<>(pageNum, pageSize);
        ReservationsParam p = new ReservationsParam();
        p.setUsername(username);
        p.setNickName(nickName);
        p.setDate(date);
        p.setEventName(eventName);
        p.setUserId(userId);
        p.setVenueId(venueId);
        p.setVenueName(venueName);
        p.setRole(role);
        p.setStatus(status);
        Page<Reservations> aPage = reservationsMapper.selectPageVo(new Page<>(pageNum, pageSize), p );
        return new Result(200, "查询成功！", aPage);
    }

    /**
     * 根据ID获取预约详情
     *
     * @param id 预约ID
     * @return 预约详情
     */
//    @GetMapping(value = "/{id}")
//    @Operation(summary = "根据预约ID获取预约信息", description = "根据预约ID查询预约的详细信息")
//    @ApiResponse(responseCode = "200", description = "获取预约信息成功")
//    @ApiResponse(responseCode = "404", description = "获取预约信息失败,该预约不存在")
//    public Result getById(@PathVariable("id") String id) {
//        Reservations reservation = reservationsService.getById(id);
//        if (reservation == null) {
//            return new Result(404, "预约不存在！", null);
//        }
//        return new Result(200, "查询成功！", reservation);
//    }

    /**
     * 创建预约
     *
     * @param params 预约对象
     * @return 创建结果
     */
    @PostMapping(value = "/create")
    @Operation(summary = "创建预约", description = "创建新预约")
    @ApiResponse(responseCode = "200", description = "创建预约成功")
    @ApiResponse(responseCode = "500", description = "创建预约失败")
    public Result create(@RequestBody Reservations params) {
        try {
            Boolean flag = reservationsService.save(params);
            if (flag == null || !flag) {
                return new Result(500, "增加失败，未知错误！", null);
            }

            return new Result(200, "增加成功！", null);
        } catch (Exception e) {
            return new Result(500, "增加失败，系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 根据ID删除预约
     *
     * @param id 预约ID
     * @return 删除结果
     */
//    @DeleteMapping(value = "/delete/{id}")
//    @Operation(summary = "删除预约", description = "根据预约ID删除预约")
//    @ApiResponse(responseCode = "200", description = "删除预约成功")
//    @ApiResponse(responseCode = "404", description = "删除预约失败,该预约不存在")
//    public Result delete(@PathVariable("id") String id) {
//        try {
//            Boolean flag = reservationsService.removeById(id);
//            if (flag == null) {
//                return new Result(500, "删除失败，未知错误！", null);
//            }
//            if (flag) {
//                return new Result(200, "删除成功！", null);
//            } else {
//                return new Result(404, "删除失败，预约不存在！", null);
//            }
//        } catch (Exception e) {
//            return new Result(500, "删除失败，系统错误：" + e.getMessage(), null);
//        }
//    }

    /**
     * 更新预约
     *
     * @param params 预约对象
     * @return 更新结果
     */
    @PutMapping(value = "/update")
    @Operation(summary = "更新预约信息", description = "根据预约ID更新预约的信息")
    @ApiResponse(responseCode = "200", description = "更新预约信息成功")
    @ApiResponse(responseCode = "404", description = "更新预约信息失败,该预约不存在")
    public Result update(@RequestBody Reservations params) {
        try {
            Boolean flag = reservationsService.updateById(params);
            if (flag == null) {
                return new Result(500, "修改失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "修改成功！", null);
            } else {
                return new Result(404, "修改失败，预约不存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "修改失败，系统错误：" + e.getMessage(), null);
        }
    }



}